package com.sx.zongzhi.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.PopulationMoveDetailBean;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.sx.zongzhi.R.id.tv_CredNum;


/**
 * Created by XZY on 2017/11/28.
 * 流入信息编辑或添加
 */
public class MoveInforEditFragment extends Fragment implements IPublicBeanView{

    @BindView(R.id.sp_HomeType) Spinner sp_HomeType;
    @BindView(R.id.sp_InReason) Spinner sp_InReason;
    @BindView(R.id.sp_CredType) Spinner sp_CredType;
    @BindView(tv_CredNum) EditText et_CredNum;
    @BindView(R.id.tv_RegData) TextView tv_RegData;
    @BindView(R.id.tv_EndData) TextView tv_EndData;
    @BindView(R.id.rbt_Yes) RadioButton rbt_Yes;
    @BindView(R.id.rbt_No) RadioButton rbt_No;
    private PopulationMoveDetailBean detailBean;
    private boolean isEdit;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_move_infor_edit,container,false);
        detailBean= (PopulationMoveDetailBean) getArguments().getSerializable("PopulationMoveDetailBean");
        isEdit=getArguments().getBoolean("isEdit");
        ButterKnife.bind(this,view);
        initData();
        showViews();
        return view;
    }

    @OnClick({R.id.tv_RegData,R.id.tv_EndData})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_RegData:
                BaseUtil.createDataPop(getActivity(),tv_RegData);
                break;
            case R.id.tv_EndData:
                BaseUtil.createDataPop(getActivity(),tv_EndData);
                break;
        }
    }

    private void initData() {
        PublicListPest publicListPest=new PublicListPest(this);
        publicListPest.getPublicList("流动人口_住所类型");
        publicListPest.getPublicList("流入原因");
        publicListPest.getPublicList("证件类型");
    }

    public boolean submit(){
        String cardNum=et_CredNum.getText().toString().trim();
        String regData=tv_RegData.getText().toString();
        String endData=tv_EndData.getText().toString();
        detailBean.setCertificateNumber(cardNum);
        detailBean.setRegisterDate(regData);
        detailBean.setExpectedDatedue(endData);
        GlobalBean hBean= (GlobalBean) sp_HomeType.getSelectedItem();
        if (hBean==null){
            ToastUtils.showToast(getActivity(),"请选择住所类型");
            return false;
        }
        detailBean.setStayLocationType(hBean);
        GlobalBean iBean= (GlobalBean) sp_InReason.getSelectedItem();
        detailBean.setInflowingReason(iBean);
        GlobalBean cBean= (GlobalBean) sp_CredType.getSelectedItem();
        detailBean.setRegistrationType(cBean);
        detailBean.setIsEmphasis(rbt_Yes.isChecked()?1:0);
        return true;
    }

    private void showViews() {
        if (isEdit) {
            et_CredNum.setText(detailBean.getCertificateNumber());
            tv_RegData.setText(detailBean.getRegisterDate());
            tv_EndData.setText(detailBean.getExpectedDatedue());
            if (detailBean.getIsEmphasis() == 1) {
                rbt_Yes.setChecked(true);
            } else {
                rbt_No.setChecked(true);
            }
        }
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(getActivity(),message);
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("流动人口_住所类型".equals(publicBean.getDomainName())) {
            String id = "";
            if (detailBean != null && detailBean.getStayLocationType() != null) {
                id = detailBean.getStayLocationType().getId();
            }
            showSpinner(sp_HomeType, publicBean.getDicts(),id);
        }
        if ("流入原因".equals(publicBean.getDomainName())) {
            String id = "";
            if (detailBean != null && detailBean.getInflowingReason() != null) {
                id = detailBean.getInflowingReason().getId();
            }
            showSpinner(sp_InReason, publicBean.getDicts(),id);
        }
        if ("证件类型".equals(publicBean.getDomainName())) {
            String id = "";
            if (detailBean != null && detailBean.getRegistrationType() != null) {
                id = detailBean.getRegistrationType().getId();
            }
            showSpinner(sp_CredType, publicBean.getDicts(),id);
        }
    }

    private void showSpinner(Spinner spinner, List<GlobalBean> datas,String id) {
        SpinnerPubAdapter adapter=new SpinnerPubAdapter(getActivity(),R.layout.item_text,datas);
        spinner.setAdapter(adapter);
        if (!BaseUtil.isEmpty(id)){
            int count=datas.size();
            for (int i=0;i<count;i++){
                if (id.equals(datas.get(i).getId())){
                    spinner.setSelection(i);
                    break;
                }
            }
        }

    }
}
